<?php
namespace Cyfy\Modules\Users\Forms;

use \Cyfy\Message;
use \Cyfy\Modules\Forms;
use \Cyfy\Modules\Users\Current;
use \Cyfy\Modules\Users\Module;

/**
 * User registration form.
 *
 * @author Dandelion <dandelion8888@gmail.com>
 * @version 0.1
 */
class Register extends Forms\Form
{
    function structure()
    {
        $this -> permission("Users", "register new users");
        
        $this -> action = "user/register-success";
        $b = new Forms\Button(self :: t(320));
        $this -> addButton($b);        

        $i = new Forms\TextBox("name", self :: t(60));
        $i -> required = true;
        $i -> disableAutocomplete = true;
        $this -> addItem($i);

        $i = new Forms\TextBox("email", self :: t(50));
        $i -> format = "email";
        $i -> required = true;
        $this -> addItem($i);

        $i = new Forms\TextBox("username", self :: t(30));
        $i -> required = true;
        $i -> disableAutocomplete = true;
        $i -> minLength = 4;
        $this -> addItem($i);

        $i = new Forms\TextBox("password1", self :: t(40));
        $i -> required = true;
        $i -> disableAutocomplete = true;
        $i -> password = true;
        $i -> minLength = 5;
        $this -> addItem($i);

        $i = new Forms\TextBox("password2", self :: t(310));
        $i -> required = true;
        $i -> disableAutocomplete = true;
        $i -> password = true;
        $this -> addItem($i);
    
        
    }

    function validate()
    {
        $error = FALSE;

        //is the username used?
        $result = \dibi :: query("SELECT [uid] FROM [:cyfy:Users-users] WHERE [username] = %s", $this -> get("username"));
        if (count($result)) {
            \Cyfy\Message :: set(self :: t(301, array($this -> get("username"))), \Cyfy\Message :: ERROR);
            $error = true;
        }
        
        //is the email used?
        $result = \dibi :: query("SELECT [uid] FROM [:cyfy:Users-users] WHERE [email] = %s", $this -> get("email"));
        if (count($result)) {
            \Cyfy\Message :: set(self :: t(810), \Cyfy\Message :: ERROR);
            $error = true;
        }

        // validates username
        if (!preg_match("/^[-a-zA-Z0-9]+$/", $this -> get("username")))
        {
            \Cyfy\Message :: set(self :: t(330), \Cyfy\Message :: ERROR);
            $error = true;
        }

        // passwords
        if ($this -> get("password1") != $this -> get("password2"))
        {
            \Cyfy\Message :: set(self :: t(340), \Cyfy\Message :: ERROR);
            $error = true;
        }

        return !$error;
    }

    function submit()
    {
        // is admin
        if($this -> getTemp('inAdmin'))
        {
            $this -> onSubmit = "Cyfy.ajax('User', 'users', '#adminUsersList', '');";
            $b = new Forms\Button(self :: t(3010));
            $this -> addButton($b);
            $this -> action = "";
        }
        
        $data = array(
            'password' => Module :: hashPassword($this -> get("password1")),
            'username' => $this -> get('username'),
            'name' => $this -> get('name'),
            'email' => $this -> get('email')
        );

        // registration OK?
        if (!\dibi :: query("INSERT INTO [:cyfy:Users-users]", $data))
        {
            \Cyfy\Message :: set(self :: t(370, \Cyfy\Message :: ERROR));
            return false;
        }
        
        
        \Cyfy\Message :: set(self :: t(350));
        $subj = self :: t(380);
        
        $email = new \Cyfy\Modules\PHPMailer\PHPMailer();
        $email -> addAddress($this -> get("email"), $this -> get("name"));
        $email -> setSubject($subj);
        $email -> setContent($this -> getAll());
        $email -> setTemplate("Users", "user-registration");
        $status = $email -> post();


        return true;
    }

}
